﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using Org.Mentalis.Network;
using System.Threading;

namespace ToRTF
{
   public class WordDocHandler
    {
        Dictionary<String, Object> typeTable = new Dictionary<String, Object>();
        _Application oWord;
        public String mainWindowTitle = "elookinto";

       public void Init()
        {
            typeTable["html"] = WdSaveFormat.wdFormatHTML;
            typeTable["txt"] = WdSaveFormat.wdFormatUnicodeText;
            typeTable["rtf"] = WdSaveFormat.wdFormatRTF;
            typeTable["xps"] = WdSaveFormat.wdFormatXPS;
            typeTable["pdf"] = WdSaveFormat.wdFormatPDF;
            oWord = new Application();
            oWord.Caption = mainWindowTitle;
            oWord.Visible = false;
            oWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;
        }

        public void CleanUp()
        {
            oWord.Quit();
        }

        static Object missing = System.Reflection.Missing.Value;

        public String SaveAs(String fileName, ref string fileName2, String format)
        {
            if (fileName.Equals(fileName2) || fileName2.Length ==0 /*|| 
                fileName.Substring(fileName.LastIndexOf(".")).Equals(
                fileName2.Substring(fileName2.LastIndexOf(".")))*/)
            {
                Console.WriteLine("gost command");
                return "gost command";
            }
            object readOnly = true;
            // we can keep it false if you want to open the file but not make it invisible
            object isVisible = true;
            object openAndRepair = false;
            object noEncodingDialog = false;
            Object istrue = true;
           
            Console.WriteLine("fileName2 " + fileName2.Length +" " + fileName2);
            Object fileName1 = fileName;
          
            _Document oWordDoc = null;
            try
            {
                oWordDoc = oWord.Documents.Open(fileName1, ReadOnly: false,
                Visible: false,
                OpenAndRepair: false,
                NoEncodingDialog: true);
                object format2 = typeTable[format];
                object fileName3 = fileName2;
                Console.WriteLine("fileName2: " + fileName2.Length + " " + fileName2);
                oWordDoc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;

                /* http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document.saveas(office.11).aspx
                */

                object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
                oWordDoc.SaveAs(fileName3,
                FileFormat:format2,
                LockComments:false,
                ReadOnlyRecommended:false, 
                Encoding: encoding);
                
            }
            catch (Exception e)
            {
                Console.WriteLine("Save as error:" + e.Message);
                Console.WriteLine(fileName + ".." + fileName.Length );
                Console.WriteLine(fileName2 + ".." + fileName2.Length);
            }
            finally
            {
                
                if (oWordDoc != null) oWordDoc.Close(ref missing, ref missing, ref missing);
                
            }
            return fileName2;
        }
    }
}
